package sample2.provider;

import sample2.provider.ddl.SQLiteTableBuilder;
import sample2.provider.ddl.SQLiteViewBuilder;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * @author YUN
 */
public interface MyDatabase {
	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "sample2.provider.MyDatabase.db";
	public static final String AUTHORITY = "sample2.provider.MyDatabase";
	public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/");
	
	public final static class MyTable extends SQLiteTableBuilder {
		static final String DIR_TYPE = "vnd.android.cursor.dir/vnd.reco.MyTable";
		static final String ITEM_TYPE = "vnd.android.cursor.item/vnd.reco.MyTable";
		static final String NAME = "MyTable";

		public static final Uri CONTENT_URI = Uri.withAppendedPath(MyDatabase.BASE_CONTENT_URI, NAME);

		public MyTable(SQLiteDatabase db) {
			super(db, NAME);
			//
			addIntegerColumn(Columns._ID, CONSTRAINT_PRIMARY_KEY_AUTOINCREMENT_NOT_NULL);
			addTextColumn(Columns.NAME, CONSTRAINT_NOT_NULL);
			addIntegerColumn(Columns.AGE, CONSTRAINT_NOT_NULL);
		}

		public static interface Columns extends BaseColumns {
			/** type:Text */
			public static final String NAME = "name";
			/** type:Integer */
			public static final String AGE = "age";
		}
	}
	
	public final class MyView extends SQLiteViewBuilder {
		static final String DIR_TYPE = "vnd.android.cursor.dir/vnd.reco.MyView";
		static final String ITEM_TYPE = "vnd.android.cursor.item/vnd.reco.MyView";
		static final String NAME = "MyView";
		
		public static final Uri CONTENT_URI = Uri.withAppendedPath(MyDatabase.BASE_CONTENT_URI, NAME);

		public static interface Columns extends MyTable.Columns{
			/** type:Text */
			public static final String NAME = "name";
		}

		public MyView(SQLiteDatabase db) {
			super(db, NAME);

			addSelect(Columns._ID, "sub.%s", Columns._ID);
			addSelect(Columns.NAME, "sub.%s", Columns.NAME);
			setFrom("(SELECT %s,%s FROM %s " +
					"UNION ALL " +
					"SELECT -1, SUBSTR(%s,1,1) section FROM %s GROUP BY section) sub",
					Columns._ID,
					Columns.NAME,
					MyTable.NAME,
					Columns.NAME,
					MyTable.NAME);
			setOrderBy(Columns.NAME);
		}
	}
}
